package com.project;

import com.project.servlet.AddServlet;
import com.project.servlet.FindAllServlet;
import com.project.servlet.LoginServlet;
import com.project.servlet.TestServlet;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.Wrapper;
import org.apache.catalina.startup.Tomcat;

public class Main {
    public Main(){
        Tomcat tomcat = new Tomcat();
        //设置tomcat端口
        tomcat.setPort(8088);
        //引导http引擎
        tomcat.getConnector();

        //定义上下文
        Context context = tomcat.addContext("",null);
        //注册servlet。第一个参数为上下文对象，第二个参数为servlet名称，第三个参数为servlet实现类对象
        Wrapper wp = Tomcat.addServlet(context,"test",new TestServlet());
        //容器一启动就初始化
        wp.setLoadOnStartup(1);
        //配置servlet的访问路径
        wp.addMapping("/pw/*");

        Wrapper wp1 = Tomcat.addServlet(context,"land",new LoginServlet());
        //容器一启动就初始化
        wp1.setLoadOnStartup(2);
        //配置servlet的访问路径
        wp1.addMapping("/land");

        Wrapper wp2 = Tomcat.addServlet(context,"add",new AddServlet());
        //容器一启动就初始化
        wp2.setLoadOnStartup(3);
        //配置servlet的访问路径
        wp2.addMapping("/add");

        Wrapper wp3 = Tomcat.addServlet(context,"findAll",new FindAllServlet());
        //容器一启动就初始化
        wp3.setLoadOnStartup(4);
        //配置servlet的访问路径
        wp3.addMapping("/findAll");

        try {
            //启动tomcat
            tomcat.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Main();
    }
}
